package com.jz.digital.login

import com.jz.digital.R
import com.jz.digital.base.BasePresenter
import com.jz.digital.bean.BaseEntity
import com.jz.digital.bean.UserInfo
import com.jz.digital.listener.ApiCallback
import com.jz.digital.net.AccountManager
import com.jz.digital.utils.AccountManagerHelper
import com.jz.digital.utils.GreenDaoUtils
import com.jz.digital.utils.LogUtils
import com.jz.digital.utils.SPHelper
import com.jz.digital.utils.StringUtils

/**
 * ================================================
 * Created: WangYong
 * Date: 2024/11/24
 * Description:
 * ================================================
 */
class LoginPresenter : BasePresenter<ILoginView>() {
    /**
     * 登陆
     */
    fun login(account: String, password: String) {
        AccountManager.login(account, password, object : ApiCallback<BaseEntity<UserInfo>>() {
            override fun onSuccess(response: BaseEntity<UserInfo>?) {
                if (isAttachView) {
                    if (response != null && response.data != null) {
                        AccountManagerHelper.getInstance().userId = account
                        GreenDaoUtils.saveUserInfo(response.data)
                        SPHelper.saveLoginState(true)
                        view.onLoginSuccess()
                    } else {
                        view.onLoginFail(StringUtils.getString(R.string.login_fail))
                    }
                }

            }

            override fun onError(t: Throwable?, code: Int, message: String?) {
                if (isAttachView) {
                    view.onLoginFail(StringUtils.getString(R.string.login_fail))
                }
                LogUtils.getInstance().writeLogAsync(message + "------t:" + t.toString())
            }

        })
    }
}